/**
 *
 */
package uy.edu.um.taller2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

/**
 * @author pegardan
 *
 */
public class ConnectionMgr {

	private static final String JDBC_DRIVER = "org.hsqldb.jdbc.JDBCDriver";

	private static final String JDBC_USER = "SA";

	private static final String JDBC_PASSWORD = "";

	private static final String JDBC_URL = "jdbc:hsqldb:file:data/um-test";

	private Connection connection = null;

	private static ConnectionMgr instance = null;

	public static ConnectionMgr getInstance() {
		if (instance == null) {

			instance = new ConnectionMgr();

		}

		return instance;
	}

	private DataSource datasource = null;

	private ConnectionMgr() {
		Properties propiedades = new Properties();

		try {

			propiedades.load(new FileInputStream("datasource.properties"));

			datasource = BasicDataSourceFactory.createDataSource(propiedades);

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		datasource = new BasicDataSource();
//
//		BasicDataSource oBasicDataSource = (BasicDataSource) datasource;
//
//		oBasicDataSource.setDriverClassName(JDBC_DRIVER);
//		oBasicDataSource.setUsername(JDBC_USER);
//		oBasicDataSource.setPassword(JDBC_PASSWORD);
//		oBasicDataSource.setUrl(JDBC_URL);
//
//		oBasicDataSource.setInitialSize(5);
//		oBasicDataSource.setMaxActive(10);
//		oBasicDataSource.setMinIdle(5);

	}

	public Connection getConnection() throws SQLException {
		return datasource.getConnection();
	}

	public void closeDatasource() throws SQLException {
		//datasource.close();
	}

}
